package com.lyl.servicestatistics.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyl.commonutils.DateUtils;
import com.lyl.commonutils.R;
import com.lyl.servicestatistics.entity.StatisticsDaily;
import com.lyl.servicestatistics.service.StatisticsDailyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author 雷艳玲
 * @since 2022-06-29
 */
@RestController
@RequestMapping("/servicestatistics/daily")
public class StatisticsDailyController {

    @Autowired
    StatisticsDailyService dailyService;

    /**
     * 生成当天数据
     * @return
     */
    @GetMapping("/initData")
    public R test(){
        // 当天 格式化 yyyy-MM-dd
//        String day = DateUtils.formatDate(new Date(), 0);// 2022-7-11
//        dailyService.createStatisticsByDay(day);
        dailyService.initData();
        return R.ok();
    }

    /**
     * 生成当天数据
     * @return
     */
    @GetMapping("/createDate")
    public R createDate(){
        // 当天 格式化 yyyy-MM-dd
        String day = DateUtils.formatDate(new Date(), 0);// 2022-7-11
        dailyService.createStatisticsByDay(day);
        return R.ok();
    }

    /*
       读取范围数据
     */
    @GetMapping("/{statrTime}/{endTime}")
    public R getDataByBetween(@PathVariable("statrTime") String statrTime,
                             @PathVariable("endTime") String endTime){
        Map<String,Object> datas= dailyService.getDataByBetween(statrTime,endTime);
        return R.ok().data(datas);
    }

    /*
        获取 月数据, 周数据, 季节数据, 年数据
  */
    @GetMapping("/getDataByType/{type}")
    public R getDataByMouth( @PathVariable("type") String type){

        Map<String,Object> datas= dailyService.getDataByType(type);
        return R.ok().data(datas);
    }



    @PostMapping("/saveAsExcel")
    public R saveAsExcel(@RequestBody Map<String,Object> obj) {

        System.out.println("map:"+obj);
        String url = dailyService.writeData(obj);
        return R.ok().message("保存成功:" + url);
    }
}

